package com.jk.common.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.jk.common.enums.WitmoreErrorCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * Description: 自定义响应类
 * Author: 豆子高
 * Date: 2025/2/24 11:55
 */
@Schema(name = "ResponseResult", description = "响应类")
@Data
public class ResponseResult<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "状态码")
    private int code;

    @Schema(description = "消息")
    private String message;

    @Schema(description = "返回的数据")
    private T data;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Schema(description = "接口调用时间")
    private LocalDateTime timestamp;

    public ResponseResult(){}
    public ResponseResult(int code, String message) {
        this.code = code;
        this.message = message;
    }
    /**
     * 成功响应（无数据）
     */
    public static <T> ResponseResult<T> success() {
        return success(null);
    }

    /**
     * 成功响应（带数据）
     */
    public static <T> ResponseResult<T> success(T data) {
        ResponseResult<T> response = new ResponseResult<>();
        response.setCode(WitmoreErrorCode.SUCCESS.getCode());
        response.setMessage("success");
        response.setData(data);
        response.setTimestamp(LocalDateTime.now());
        return response;
    }

    /**
     * 失败响应
     */
    public static <T> ResponseResult<T> fail(int code, String message) {
        ResponseResult<T> response = new ResponseResult<>();
        response.setCode(code);
        response.setMessage(message);
        response.setTimestamp(LocalDateTime.now());
        return response;
    }


    /**
     * 失败响应
     */
    public static <T> ResponseResult<T> fail(WitmoreErrorCode errorCode) {
        ResponseResult<T> response = new ResponseResult<>();
        response.setCode(errorCode.getCode());
        response.setMessage(errorCode.getMessage());
        response.setTimestamp(LocalDateTime.now());
        return response;
    }

    /**
     * 失败响应（带数据）
     */
    public static <T> ResponseResult<T> fail(int code, String message, T data) {
        ResponseResult<T> response = new ResponseResult<>();
        response.setCode(code);
        response.setMessage(message);
        response.setData(data);
        response.setTimestamp(LocalDateTime.now());
        return response;
    }
}